package see.battle.gui;

import java.io.IOException;
import java.util.concurrent.Callable;

import see.battle.ApplicationControler;
import see.battle.Client;
import see.battle.LifeCycle;
import see.battle.drawer.AndroidDrawer;
import see.battle.drawer.Drawer;
import see.battle.object.ObjectPool;
import see.battle.server.Server;
import android.app.Activity;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

public class Game extends Activity {
  private Drawer _drawer;
  private ObjectPool _pool;
  private GameView _gameView;
  private Client _client;
  private LifeCycle _lifeCycle;
  private Thread _lifeThread;
  private int _gameMode;
  
  public class ResultProcessor implements Callable<Object> {

    int _hitNumber = 0;
    int _opponentHitNumber = -1;
    
    public void setHitNumber(int hitNumber) {
      _hitNumber = hitNumber;
    }
  
    public void setOpponentHitNumber(int opponentHitNumber) {
      _opponentHitNumber = opponentHitNumber;
    }

    //@Override
    public Object call() throws Exception {
      callResult(_hitNumber, _opponentHitNumber);
      return null;
    }
  }
  
  //@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) {
      _gameMode = extras.getInt("GAME_MODE");
    }
  }

  //@Override
  public void onStart() {
    super.onStart();
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    _gameView = new GameView(this);
    _drawer = new AndroidDrawer(getResources(), metrics, _gameView);
    _pool = new ObjectPool(_drawer, _gameMode);
    ApplicationControler ac = (ApplicationControler)getApplicationContext();
    Server server = ac.get_server();
    BluetoothSocket bs = ac.get_socket();
		try {
      _client = new Client(_pool, _gameMode, server, bs);
    } catch (IOException e) {
      e.printStackTrace();
    }
    _client.set_resultProcessor(new ResultProcessor());
    if (server != null) {
      server.set_resultProcessor(new ResultProcessor());
    }
	  _lifeCycle = new LifeCycle(_pool, _client);
    _lifeCycle.set_resultProcessor(new ResultProcessor());
    _lifeThread = new Thread(_lifeCycle);
    _lifeThread.start();
    setContentView(_gameView);
    _client.startAdaptor();
  }
  
  public void callResult(int hitNumber, int opponentHitNumber) {
    _client.stopAdaptor();
    ApplicationControler ac = (ApplicationControler)getApplicationContext();
    Server server = ac.get_server();
    if (server != null) server.stop();
    Intent intent = new Intent(this, Result.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    intent.putExtra("HIT_NUMBER", hitNumber);
    intent.putExtra("OPPONENT_HIT_NUMBER", opponentHitNumber);
    intent.putExtra("GAME_MODE", _gameMode);
    finish();
    startActivity(intent);
  }
}